//
//  SHComposeToolbarView.swift
//  SHWeibo01
//
//  Created by 卫平玉 on 16/7/28.
//  Copyright © 2016年 卫平玉. All rights reserved.
//

import UIKit

enum SHComposeToolbarViewType: Int{
    
    // 图片
    case Picture = 1001
    // @
    case Mention = 1002
    // #
    case Trend = 1003
    // 表情
    case Emoticon = 1004
    // +
    case Add = 1005
}
class SHComposeToolbarView: UIView {

    //表情键盘
    var emoticonButton: UIButton?
    //是否是表情键盘
    var isEmoticon: Bool = false {
        didSet{
            
//            let imgName = isEmoticon ?
//            "compose_keyboardbutton_background":
//            "compose_emoticonbutton_background"
//            
//            emoticonButton?.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
//            emoticonButton?.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
            let imgName = isEmoticon ? "compose_keyboardbutton_background" : "compose_emoticonbutton_background"
            emoticonButton?.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
            emoticonButton?.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
            
        }
    }
    
    
//    var closure:((type: SHComposeToolbarViewType)->())?
    var closure:((type: SHComposeToolbarViewType)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    func buttonClick(button: UIButton){
//        
//        closure?(type: SHComposeToolbarViewType(rawValue: button.tag)!)
//    }
    func buttonClick(button: UIButton){
        // 执行闭包
        closure?(type: SHComposeToolbarViewType(rawValue: button.tag)!)
    }
    
    private func setupUI(){
        
        backgroundColor = RandomColor()
        
        // 添加控件
        let pictureButton = addChildButton("compose_toolbar_picture",type: .Picture)
        let mentionButton = addChildButton("compose_mentionbutton_background",type: .Mention)
        let trendButton = addChildButton("compose_trendbutton_background",type: .Trend)
         emoticonButton = addChildButton("compose_emoticonbutton_background",type: .Emoticon)
        let addButton = addChildButton("compose_add_background",type: .Add)
        
        //添加约束
        pictureButton.snp_makeConstraints { (make) -> Void in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        mentionButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        
        trendButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton!)
        }
        emoticonButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        
        addButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
        }
    }
    
    
    func addChildButton(imgName: String,type: SHComposeToolbarViewType) -> UIButton{
        
        let button = UIButton()
        
        button.tag = type.rawValue
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setImage(UIImage(named:imgName), forState: UIControlState.Normal)

        button.setImage(UIImage(named:"\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        addSubview(button)
        return button
    }

}
